package com.neu.cloudfactory.controler;

import com.neu.cloudfactory.bean.Factory.Factory;
import com.neu.cloudfactory.bean.Order.Ordering;
import com.neu.cloudfactory.bean.Scheduling.Scheduling;
import com.neu.cloudfactory.bean.Tender.Tender;
import com.neu.cloudfactory.bean.Tender.TenderResult;
import com.neu.cloudfactory.bean.User.User;
import com.neu.cloudfactory.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
public class TenderControler {
    @Autowired
    TenderRepository tenderRepository;
    @Autowired
    FactoryRepository factoryRepository;
    @Autowired
    UserRepository userRepository;
    @Autowired
    OrderRepository orderRepository;
    @Autowired
    SchedulingRepository schedulingRepository;

    @RequestMapping("/tender/findAllByOrderringid/{orderringid}")
    public List<TenderResult> findAllByOrderringid (@PathVariable("orderringid") Integer orderid){
        List<Tender> tenders = tenderRepository.findAllByOrderid(orderid);
        List<TenderResult> tenderResults = new ArrayList<>();

        Factory factory;
        User manager;
        TenderResult tmp;
        for (Tender tender : tenders) {
            try {
                factory = factoryRepository.getById(tender.getFactoryid());
                manager = userRepository.getById(factory.getManagerid());

                tmp = new TenderResult();
                tmp.setId(tender.getId());
                tmp.setFactoryname(factory.getName());
                tmp.setManager(manager.getName());
                tmp.setConnection(manager.getConnection());
                tmp.setQuote(tender.getQuote());

                tenderResults.add(tmp);
            }catch (Exception e){
                continue;
            }
        }
        return tenderResults;
    }

    @RequestMapping("/tender/selecttender/{id}/{tenderid}")
    public String selectTender(@PathVariable("id") Integer id,@PathVariable("tenderid") Integer tenderid){
        Ordering order = orderRepository.getById(id);
        Tender tender = tenderRepository.getById(tenderid);
        Scheduling scheduling = new Scheduling();
        if(order.getStatusid()==3){
            tender.setStatusid(2);
            tender=tenderRepository.save(tender);

            order.setTenderid(tenderid);
            order.setStatusid(4);

            scheduling.setOrderid(id);
            scheduling.setFactoryid(tender.getFactoryid());
            schedulingRepository.save(scheduling);
            return "success";
        }
        return "failed";
    }

    @RequestMapping("/tender/factorytender/{factoryid}")
    public List<Tender> factoryTenders(@PathVariable("factoryid") Integer factoryid){
        return tenderRepository.findAllByFactoryid(factoryid);
    }

    @RequestMapping("/tender/save")
    public String save(@RequestBody Tender tender){
        Optional<Tender> tmp = tenderRepository.findByFactoryidAndOrderid(tender.getFactoryid(),tender.getOrderid());
        if(tmp.isPresent()){
            tender.setId( tmp.get().getId() );
        }
        tender=tenderRepository.save(tender);

        Ordering ordering = orderRepository.getById(tender.getOrderid());
        if(ordering.getStatusid()==2){
            ordering.setStatusid(3);
            ordering = orderRepository.save(ordering);
        }

        return tender.getId()!=null&&ordering.getStatusid()==3?"success":"failed";
    }
}
